package com.jeethink.erp.transfer.controller;

import java.util.List;

import com.jeethink.common.core.domain.entity.SysUser;
import com.jeethink.common.core.domain.model.LoginUser;
import com.jeethink.common.utils.ServletUtils;
import com.jeethink.common.utils.spring.SpringUtils;
import com.jeethink.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jeethink.common.annotation.Log;
import com.jeethink.common.core.controller.BaseController;
import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.enums.BusinessType;
import com.jeethink.erp.transfer.domain.TbStockTransfer;
import com.jeethink.erp.transfer.service.ITbStockTransferService;
import com.jeethink.common.utils.poi.ExcelUtil;
import com.jeethink.common.core.page.TableDataInfo;

/**
 * 库存调拨申请Controller
 * 
 * @author ypr
 * @date 2021-06-03
 */
@RestController
@RequestMapping("/erp/transfer")
public class TbStockTransferController extends BaseController
{
    @Autowired
    private ITbStockTransferService tbStockTransferService;

    /**
     * 查询库存调拨申请列表
     */
    @PreAuthorize("@ss.hasPermi('erp:transfer:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbStockTransfer tbStockTransfer)
    {
        startPage();
        List<TbStockTransfer> list = tbStockTransferService.selectTbStockTransferList(tbStockTransfer);
        return getDataTable(list);
    }

    /**
     * 导出库存调拨申请列表
     */
    @PreAuthorize("@ss.hasPermi('erp:transfer:export')")
    @Log(title = "库存调拨申请", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbStockTransfer tbStockTransfer)
    {
        List<TbStockTransfer> list = tbStockTransferService.selectTbStockTransferList(tbStockTransfer);
        ExcelUtil<TbStockTransfer> util = new ExcelUtil<TbStockTransfer>(TbStockTransfer.class);
        return util.exportExcel(list, "transfer");
    }

    /**
     * 获取库存调拨申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp:transfer:query')")
    @GetMapping(value = "/{transferId}")
    public AjaxResult getInfo(@PathVariable("transferId") Long transferId)
    {
        return AjaxResult.success(tbStockTransferService.selectTbStockTransferById(transferId));
    }

    /**
     * 新增库存调拨申请
     */
    @Log(title = "库存调拨申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbStockTransfer tbStockTransfer)
    {
        return toAjax(tbStockTransferService.insertTbStockTransfer(tbStockTransfer));
    }

    /**
     * 修改库存调拨申请
     */
    @PreAuthorize("@ss.hasPermi('erp:transfer:edit')")
    @Log(title = "库存调拨申请", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED,readOnly = false,rollbackFor = {Exception.class})
    public AjaxResult edit(@RequestBody TbStockTransfer tbStockTransfer)
    {
        return tbStockTransferService.updateTbStockTransfer(tbStockTransfer);
    }

    /**
     * 删除库存调拨申请
     */
    @PreAuthorize("@ss.hasPermi('erp:transfer:remove')")
    @Log(title = "库存调拨申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{transferIds}")
    public AjaxResult remove(@PathVariable Long[] transferIds)
    {
        return toAjax(tbStockTransferService.deleteTbStockTransferByIds(transferIds));
    }

    @GetMapping("/getMyTransferList")
    public TableDataInfo getMyTransferList(TbStockTransfer tbStockTransfer){

        //获取当前登录用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tbStockTransfer.setShopId(loginUser.getUser().getShopId());
        startPage();
        List<TbStockTransfer> list = tbStockTransferService.getMyTransferList(tbStockTransfer);
        return getDataTable(list);
    }

}
